#include<iostream>
#include<queue>
#include<vector>
#include<stdlib.h>
#include<stdio.h>

using namespace std;

priority_queue< long long ,vector<long long>,greater<long long> > q;

int main()
{
	int n , x ;
	long long cost ;
	cin>>n;
    	cost = 0; 
    	for(int i=0;i<n;i++)
    	{
    		cin>>x;
    		q.push(x);
    	}
    	long long first , second;
    	for(int i=1;i<n;i++)
    	{
    		first = q.top();
    		q.pop();
    		second = q.top();
    		q.pop();
    		cost+=(first+second);
    		q.push(first+second);
    	}
    	cout<<cost<<endl;
	//system("pause");
	return 0;
}

